<?php

class Login extends Controller {

    function index()
    {
        //$data['main_content'] = 'login_form';
        //$data['main_content']='password_form';
        //$this->load->view('includes/template', $data);
        $this->load->view('password_form');
    }

    function validate_credentials()
    {
        $this->load->library('session');

        $this->load->model('membership_model');
        $query = $this->membership_model->validate();

        if($query) // if the user's credentials validated...
        {
            $username =  $this->input->post('username');
            $data = array(
                'username' => $username,
                'es_administrador'=> $this->membership_model->es_administrador($username),
                'es_operador'=> $this->membership_model->es_operador($username)
            );
            $this->session->set_userdata($data);

            if ($data['es_administrador'])
            {

                echo "template_admin";
            }
        else
           {
              echo "template_operador";
           }
        }
        else // incorrect username or password
        {
            echo "template_operador";

        }

    }

    function signup()
    {
       // $data['main_content'] = 'signup_form';
       // $this->load->view('includes/template', $data);
         $this->load->view('signup_form');
    }

    function create_member()
    {
        $this->load->library('form_validation');

        // field name, error message, validation rules
        $this->form_validation->set_rules('nombre', 'nombre', 'trim|required');
        $this->form_validation->set_rules('apellido', 'apellido', 'trim|required');
        //$this->form_validation->set_rules('email_address', 'Email Address', 'trim|required|valid_email');
        $this->form_validation->set_rules('loginn', 'loginn', 'trim|required|min_length[4]');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
        $this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]');


        if($this->form_validation->run() == FALSE)
        {
            $this->load->view('signup_form');
        }

        else
        {
            $this->load->model('membership_model');

            if($query = $this->membership_model->create_member())
            {
                $this->load->view('signup_successful');
            }
            else
            {
                $this->load->view('signup_form');
            }
        }

    }

    function logout()
    {
       // $this->session->sess_destroy();
        $this->index();
    }

}